{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "找出了比较好的学习率之后，我们再尝试调整正则化因子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "正则化因子的初始值是7e-5，现在尝试增大正则化因子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-2335cefcf613>:14: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From <ipython-input-1-2335cefcf613>:79: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See @{tf.nn.softmax_cross_entropy_with_logits_v2}.\n",
      "\n",
      "step 100, entropy loss: 0.323242, l2_loss: 12538.234375, total loss: 12.861477\n",
      "0.91\n",
      "step 200, entropy loss: 0.265405, l2_loss: 12291.686523, total loss: 12.557093\n",
      "0.94\n",
      "step 300, entropy loss: 0.166077, l2_loss: 12049.574219, total loss: 12.215652\n",
      "0.98\n",
      "step 400, entropy loss: 0.234331, l2_loss: 11811.999023, total loss: 12.046330\n",
      "0.96\n",
      "step 500, entropy loss: 0.171041, l2_loss: 11579.014648, total loss: 11.750055\n",
      "0.99\n",
      "step 600, entropy loss: 0.185559, l2_loss: 11350.796875, total loss: 11.536357\n",
      "0.98\n",
      "step 700, entropy loss: 0.083024, l2_loss: 11127.053711, total loss: 11.210077\n",
      "1.0\n",
      "step 800, entropy loss: 0.126959, l2_loss: 10907.662109, total loss: 11.034621\n",
      "0.95\n",
      "step 900, entropy loss: 0.110997, l2_loss: 10692.540039, total loss: 10.803537\n",
      "0.98\n",
      "step 1000, entropy loss: 0.058511, l2_loss: 10481.781250, total loss: 10.540294\n",
      "1.0\n",
      "0.9687\n",
      "step 1100, entropy loss: 0.068964, l2_loss: 10274.955078, total loss: 10.343920\n",
      "0.98\n",
      "step 1200, entropy loss: 0.159669, l2_loss: 10072.550781, total loss: 10.232221\n",
      "0.98\n",
      "step 1300, entropy loss: 0.023551, l2_loss: 9873.913086, total loss: 9.897465\n",
      "0.99\n",
      "step 1400, entropy loss: 0.030374, l2_loss: 9679.271484, total loss: 9.709645\n",
      "1.0\n",
      "step 1500, entropy loss: 0.041197, l2_loss: 9488.531250, total loss: 9.529729\n",
      "0.98\n",
      "step 1600, entropy loss: 0.123708, l2_loss: 9301.371094, total loss: 9.425079\n",
      "0.96\n",
      "step 1700, entropy loss: 0.024744, l2_loss: 9118.140625, total loss: 9.142885\n",
      "1.0\n",
      "step 1800, entropy loss: 0.079693, l2_loss: 8938.438477, total loss: 9.018132\n",
      "0.99\n",
      "step 1900, entropy loss: 0.011238, l2_loss: 8762.269531, total loss: 8.773508\n",
      "0.99\n",
      "step 2000, entropy loss: 0.079892, l2_loss: 8589.545898, total loss: 8.669438\n",
      "0.98\n",
      "0.9793\n",
      "step 2100, entropy loss: 0.057722, l2_loss: 8420.313477, total loss: 8.478036\n",
      "0.99\n",
      "step 2200, entropy loss: 0.045329, l2_loss: 8254.207031, total loss: 8.299537\n",
      "0.99\n",
      "step 2300, entropy loss: 0.045762, l2_loss: 8091.731934, total loss: 8.137494\n",
      "1.0\n",
      "step 2400, entropy loss: 0.020635, l2_loss: 7932.293457, total loss: 7.952929\n",
      "0.99\n",
      "step 2500, entropy loss: 0.031860, l2_loss: 7775.894531, total loss: 7.807755\n",
      "1.0\n",
      "step 2600, entropy loss: 0.062146, l2_loss: 7622.773438, total loss: 7.684919\n",
      "1.0\n",
      "step 2700, entropy loss: 0.045833, l2_loss: 7472.754395, total loss: 7.518588\n",
      "1.0\n",
      "step 2800, entropy loss: 0.023311, l2_loss: 7325.600098, total loss: 7.348912\n",
      "1.0\n",
      "step 2900, entropy loss: 0.087674, l2_loss: 7181.378906, total loss: 7.269053\n",
      "0.99\n",
      "step 3000, entropy loss: 0.050463, l2_loss: 7039.963379, total loss: 7.090427\n",
      "1.0\n",
      "0.9825\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "\n",
    "data_dir = '/tmp/tensorflow/mnist/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])\n",
    "learning_rate = tf.placeholder(tf.float32)\n",
    "\n",
    "#构造一个reshape函数，重构输入图片数据大小\n",
    "with tf.name_scope('reshape'):\n",
    "    x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
    "\n",
    "#第一个卷积层\n",
    "with tf.name_scope('conv1'):\n",
    "    shape = [6, 6, 1, 32]  \n",
    "    W_conv1 = tf.Variable(tf.truncated_normal(shape, stddev=0.1),\n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    shape = [32]\n",
    "    b_conv1 = tf.Variable(tf.constant(0.1, shape=shape))\n",
    "    l_conv1 = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv1\n",
    "    h_conv1 = tf.nn.relu(l_conv1)\n",
    "\n",
    "with tf.name_scope('pool1'):\n",
    "    h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('conv2'):\n",
    "    W_conv2 = tf.Variable(tf.truncated_normal([6,6 , 32, 64], stddev=0.1),\n",
    "                        \n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))\n",
    "    l_conv2 = tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv2\n",
    "    h_conv2 = tf.nn.relu(l_conv2)\n",
    "\n",
    "\n",
    "with tf.name_scope('pool2'):\n",
    "    h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('fc1'):\n",
    "    W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1),\n",
    "                      \n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))\n",
    "\n",
    "    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])\n",
    "    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\n",
    "\n",
    "\n",
    "with tf.name_scope('dropout'):\n",
    "    keep_prob = tf.placeholder(tf.float32)\n",
    "    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\n",
    "\n",
    "\n",
    "with tf.name_scope('fc2'):\n",
    "    W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1),\n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))\n",
    "\n",
    "    y = tf.matmul(h_fc1_drop, W_fc2) + b_fc2\n",
    "\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "\n",
    "l2_loss = tf.add_n( [tf.nn.l2_loss(w) for w in tf.get_collection('WEIGHTS')] )\n",
    "total_loss = cross_entropy + 1e-3*l2_loss                                             #将正则化因子调整为1e-3\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)\n",
    "\n",
    "\n",
    "for step in range(3000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    lr = 0.1                                                                      #调整学习率为0.1\n",
    "    _, loss, l2_loss_value, total_loss_value = sess.run(\n",
    "               [train_step, cross_entropy, l2_loss, total_loss], \n",
    "               feed_dict={x: batch_xs, y_: batch_ys, learning_rate:lr, keep_prob:0.5})\n",
    "  \n",
    "    if (step+1) % 100 == 0:\n",
    "        print('step %d, entropy loss: %f, l2_loss: %f, total loss: %f' % \n",
    "            (step+1, loss, l2_loss_value, total_loss_value))\n",
    "    # Test trained model\n",
    "        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "        print(sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys, keep_prob:0.5}))\n",
    "    if (step+1) % 1000 == 0:\n",
    "        print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                    y_: mnist.test.labels, keep_prob:0.5}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "事实上，最后的结果相差不多，调整后稍微差一些"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "调小正则参数为1e-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting /tmp/tensorflow/mnist/input_data\\train-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting /tmp/tensorflow/mnist/input_data\\t10k-labels-idx1-ubyte.gz\n",
      "step 100, entropy loss: 0.404953, l2_loss: 38214.531250, total loss: 4.226406\n",
      "0.89\n",
      "step 200, entropy loss: 0.193385, l2_loss: 38140.164062, total loss: 4.007401\n",
      "0.95\n",
      "step 300, entropy loss: 0.092889, l2_loss: 38064.906250, total loss: 3.899379\n",
      "0.98\n",
      "step 400, entropy loss: 0.228373, l2_loss: 37989.882812, total loss: 4.027361\n",
      "0.98\n",
      "step 500, entropy loss: 0.155947, l2_loss: 37914.683594, total loss: 3.947415\n",
      "0.96\n",
      "step 600, entropy loss: 0.050012, l2_loss: 37839.832031, total loss: 3.833996\n",
      "0.97\n",
      "step 700, entropy loss: 0.068647, l2_loss: 37764.898438, total loss: 3.845137\n",
      "1.0\n",
      "step 800, entropy loss: 0.179853, l2_loss: 37690.062500, total loss: 3.948859\n",
      "0.98\n",
      "step 900, entropy loss: 0.137887, l2_loss: 37615.382812, total loss: 3.899425\n",
      "0.98\n",
      "step 1000, entropy loss: 0.077787, l2_loss: 37540.835938, total loss: 3.831870\n",
      "0.97\n",
      "0.9676\n",
      "step 1100, entropy loss: 0.043949, l2_loss: 37466.417969, total loss: 3.790590\n",
      "0.99\n",
      "step 1200, entropy loss: 0.077626, l2_loss: 37392.234375, total loss: 3.816849\n",
      "0.97\n",
      "step 1300, entropy loss: 0.104478, l2_loss: 37318.105469, total loss: 3.836289\n",
      "0.98\n",
      "step 1400, entropy loss: 0.075923, l2_loss: 37244.093750, total loss: 3.800332\n",
      "0.99\n",
      "step 1500, entropy loss: 0.012422, l2_loss: 37170.183594, total loss: 3.729441\n",
      "0.99\n",
      "step 1600, entropy loss: 0.073447, l2_loss: 37096.468750, total loss: 3.783093\n",
      "0.96\n",
      "step 1700, entropy loss: 0.038863, l2_loss: 37022.875000, total loss: 3.741150\n",
      "0.99\n",
      "step 1800, entropy loss: 0.144612, l2_loss: 36949.351562, total loss: 3.839547\n",
      "0.97\n",
      "step 1900, entropy loss: 0.146331, l2_loss: 36876.183594, total loss: 3.833950\n",
      "0.97\n",
      "step 2000, entropy loss: 0.105615, l2_loss: 36802.863281, total loss: 3.785901\n",
      "0.98\n",
      "0.9777\n",
      "step 2100, entropy loss: 0.141564, l2_loss: 36729.785156, total loss: 3.814543\n",
      "1.0\n",
      "step 2200, entropy loss: 0.074936, l2_loss: 36656.937500, total loss: 3.740630\n",
      "0.99\n",
      "step 2300, entropy loss: 0.031357, l2_loss: 36584.320312, total loss: 3.689789\n",
      "0.99\n",
      "step 2400, entropy loss: 0.173980, l2_loss: 36511.539062, total loss: 3.825134\n",
      "0.99\n",
      "step 2500, entropy loss: 0.077479, l2_loss: 36439.054688, total loss: 3.721384\n",
      "0.99\n",
      "step 2600, entropy loss: 0.009101, l2_loss: 36366.769531, total loss: 3.645778\n",
      "1.0\n",
      "step 2700, entropy loss: 0.014567, l2_loss: 36294.531250, total loss: 3.644019\n",
      "1.0\n",
      "step 2800, entropy loss: 0.012775, l2_loss: 36222.582031, total loss: 3.635033\n",
      "0.99\n",
      "step 2900, entropy loss: 0.057462, l2_loss: 36150.539062, total loss: 3.672516\n",
      "1.0\n",
      "step 3000, entropy loss: 0.014202, l2_loss: 36078.851562, total loss: 3.622087\n",
      "0.99\n",
      "0.9828\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "\n",
    "data_dir = '/tmp/tensorflow/mnist/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])\n",
    "learning_rate = tf.placeholder(tf.float32)\n",
    "\n",
    "#构造一个reshape函数，重构输入图片数据大小\n",
    "with tf.name_scope('reshape'):\n",
    "    x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
    "\n",
    "#第一个卷积层\n",
    "with tf.name_scope('conv1'):\n",
    "    shape = [6, 6, 1, 32]  \n",
    "    W_conv1 = tf.Variable(tf.truncated_normal(shape, stddev=0.1),\n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    shape = [32]\n",
    "    b_conv1 = tf.Variable(tf.constant(0.1, shape=shape))\n",
    "    l_conv1 = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv1\n",
    "    h_conv1 = tf.nn.relu(l_conv1)\n",
    "\n",
    "with tf.name_scope('pool1'):\n",
    "    h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('conv2'):\n",
    "    W_conv2 = tf.Variable(tf.truncated_normal([6,6 , 32, 64], stddev=0.1),\n",
    "                        \n",
    "                        collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))\n",
    "    l_conv2 = tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], \n",
    "                         padding='SAME') + b_conv2\n",
    "    h_conv2 = tf.nn.relu(l_conv2)\n",
    "\n",
    "\n",
    "with tf.name_scope('pool2'):\n",
    "    h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],\n",
    "                        strides=[1, 2, 2, 1], padding='VALID')\n",
    "\n",
    "\n",
    "with tf.name_scope('fc1'):\n",
    "    W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1),\n",
    "                      \n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))\n",
    "\n",
    "    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])\n",
    "    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\n",
    "\n",
    "\n",
    "with tf.name_scope('dropout'):\n",
    "    keep_prob = tf.placeholder(tf.float32)\n",
    "    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\n",
    "\n",
    "\n",
    "with tf.name_scope('fc2'):\n",
    "    W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1),\n",
    "                      collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS'])\n",
    "    b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))\n",
    "\n",
    "    y = tf.matmul(h_fc1_drop, W_fc2) + b_fc2\n",
    "\n",
    "\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "\n",
    "l2_loss = tf.add_n( [tf.nn.l2_loss(w) for w in tf.get_collection('WEIGHTS')] )\n",
    "total_loss = cross_entropy + 1e-4*l2_loss                                             #将正则化因子调整为1e-3\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)\n",
    "\n",
    "\n",
    "for step in range(3000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    lr = 0.1                                                                      #调整学习率为0.1\n",
    "    _, loss, l2_loss_value, total_loss_value = sess.run(\n",
    "               [train_step, cross_entropy, l2_loss, total_loss], \n",
    "               feed_dict={x: batch_xs, y_: batch_ys, learning_rate:lr, keep_prob:0.5})\n",
    "  \n",
    "    if (step+1) % 100 == 0:\n",
    "        print('step %d, entropy loss: %f, l2_loss: %f, total loss: %f' % \n",
    "            (step+1, loss, l2_loss_value, total_loss_value))\n",
    "    # Test trained model\n",
    "        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "        print(sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys, keep_prob:0.5}))\n",
    "    if (step+1) % 1000 == 0:\n",
    "        print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                    y_: mnist.test.labels, keep_prob:0.5}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以发现，正则化因子对结果的影响不是特别大"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
